<?php

namespace App\Http\Controllers\Front;

use App\Models\OrderGoods;
use Illuminate\Http\Request;
use Session,Auth;
use App\Models\User;
use App\Models\Comment;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Interfaces\CommentService;
use Illuminate\Support\Facades\Redirect;
use App\Models\CommentPicture;
class MyCommentController extends Controller
{

    public function index(CommentService $commentService)
    {
        $user = Auth::user();
        return view('front.my.order_comment',compact('user'));
    }



    public function store(Request $request,CommentService $commentService,CommentPicture $commentPicture)
    {
        $user = Auth::user();
        //dd($request->all());
        $user_name =$user->user_name;
        $user_id = User::where('user_name',$user_name)->pluck('id');
        //dd($user_id);
        if($request->get('niming')){
            $type = 1;
        }else{
            $type = 0;
        }
        $order_goods_id = $request->get('order_goods_id');
        $star = $request->get('CommentRating');
        $content = $request->has('CommentContent')?$request->get('CommentContent'):"";
        $picStr="";
        if(mb_strlen($content)>500){
            return redirect()->back()->withInput()->withErrors('评论字数不能超过500字');
        }
        if($request->has('image_url')){
            $pictures = $request->get('image_url');
            if(count($pictures)>10){
                return redirect()->back()->withInput()->withErrors('上传图片不能超过10张');
            }
            for($i=0;$i<count($pictures);$i++){
                if($picStr){
                    $picStr = $picStr.','.$pictures[$i];
                }else{
                    $picStr = $pictures[$i];
                }
            }
        }
        $orderGoods = OrderGoods::find($order_goods_id);
        if($orderGoods->comment_status==1){
            return redirect()->back()->withInput()->withErrors('该商品已评论');
        }
        $comment =  $commentService->createComment($user_id,$order_goods_id,$star,$content,$label=null,$type,$picStr);
        if($comment){
            return view('front.my.order_comment',compact('user'));
        }else{
            return redirect()->back()->withInput()->withErrors('评论失败');
        }
    }


}
